from typing import Dict
import os

KERNEL_SCRIPT_RULES = [
    {"script": "kernel_软死锁.sh", "keywords": ["软死锁", "soft lockup", "死锁"], "weight": 1},
    {"script": "kernel_TCP.IP参数耗尽.sh", "keywords": ["tcp", "ip", "参数耗尽", "port"], "weight": 1}
]

def select_script_for_anomaly(anomaly: Dict) -> str:
    root_cause = anomaly.get("root_cause", "").lower()
    evidence = " ".join(anomaly.get("evidence", [])).lower()
    severity = anomaly.get("severity", 0)
    best_score = -1
    best_script = KERNEL_SCRIPT_RULES[0]["script"]
    for rule in KERNEL_SCRIPT_RULES:
        score = 0
        for kw in rule["keywords"]:
            if kw in root_cause or kw in evidence:
                score += rule["weight"]
        if severity >= 8:
            score += 2
        elif severity >= 6:
            score += 1
        score += len(anomaly.get("evidence", [])) * 0.5
        if score > best_score:
            best_score = score
            best_script = rule["script"]
    return best_script 